
const path = require('path')
const fs = require('fs')
function getPages (pageName) {
  const map = {}
  if (!pageName) {
    const PAGE_PATH = path.resolve(__dirname, './src/pages')
    const entryFiles = fs.readdirSync(PAGE_PATH)
    entryFiles.forEach(filePath => {
      map[filePath] = {
        entry: 'src/pages/' + filePath + '.js',
        template: 'src/pages/' + filePath + '.html',
        filename: filePath + '.html',
        chunks: ['chunk-vendors', 'chunk-common', filePath]
      }
    })
    return map
  }
  // 指定单页面打包
  map[pageName] = {
    entry: 'src/pages/' + pageName + '.js',
    template: 'src/pages/' + pageName + '.html',
    filename: pageName + '.html',
    chunks: ['chunk-vendors', 'chunk-common', pageName]
  }
  return map
}
console.log(process.env.NODE_ENV)
const rawArgv = process.argv.slice(2)
const [, , moduleName] = rawArgv
const pages = getPages(moduleName)
module.exports = {
  pages: {
    // 多页配置
    index: {
      entry: "./src/pages/index/index.js", // 配置入口js文件
      template: "./src/pages/index/index.html", // 主页面
      filename: "index.html", // 打包后的html文件名称
      title: "首页", // 打包后的.html中<title>标签的文本内容
      chunks: ['chunk-vendors', 'chunk-common', 'index']
    },
    about: {
      entry: "./src/pages/about/about.js",
      template: "./src/pages/about/about.html",
      filename: "about.html",
      title: "详情",
      desc: "关键词",
      chunks: ['chunk-vendors', 'chunk-common', 'about']
    },
    business: {
      entry: "./src/pages/business/business.js",
      template: "./src/pages/business/business.html",
      filename: "business.html",
      title: "详情",
      desc: "关键词",
      chunks: ['chunk-vendors', 'chunk-common', 'business']
    },
    customize: {
      entry: "./src/pages/customize/customize.js",
      template: "./src/pages/customize/customize.html",
      filename: "customize.html",
      title: "详情",
      desc: "关键词",
      chunks: ['chunk-vendors', 'chunk-common', 'customize']
    },
    detail: {
      entry: "./src/pages/detail/detail.js",
      template: "./src/pages/detail/detail.html",
      filename: "detail.html",
      title: "详情",
      desc: "关键词",
      chunks: ['chunk-vendors', 'chunk-common', 'detail']
    },
    introduce: {
      entry: "./src/pages/introduce/introduce.js",
      template: "./src/pages/introduce/introduce.html",
      filename: "introduce.html",
      title: "详情",
      desc: "关键词",
      chunks: ['chunk-vendors', 'chunk-common', 'introduce']
    },
    list: {
      entry: "./src/pages/list/list.js",
      template: "./src/pages/list/list.html",
      filename: "list.html",
      title: "详情",
      desc: "关键词",
      chunks: ['chunk-vendors', 'chunk-common', 'list']
    },
    market: {
      entry: "./src/pages/market/market.js",
      template: "./src/pages/market/market.html",
      filename: "market.html",
      title: "详情",
      desc: "关键词",
      chunks: ['chunk-vendors', 'chunk-common', 'market']
    },
    search: {
      entry: "./src/pages/search/search.js",
      template: "./src/pages/search/search.html",
      filename: "search.html",
      title: "详情",
      desc: "关键词",
      chunks: ['chunk-vendors', 'chunk-common', 'search']
    },
    overall: {
      entry: "./src/pages/overall/overall.js",
      template: "./src/pages/overall/overall.html",
      filename: "overall.html",
      title: "详情",
      desc: "关键词",
      chunks: ['chunk-vendors', 'chunk-common', 'overall']
    },
    suggest: {
      entry: "./src/pages/suggest/suggest.js",
      template: "./src/pages/suggest/suggest.html",
      filename: "suggest.html",
      title: "详情",
      desc: "关键词",
      chunks: ['chunk-vendors', 'chunk-common', 'suggest']
    },
    stock: {
      entry: "./src/pages/stock/stock.js",
      template: "./src/pages/stock/stock.html",
      filename: "stock.html",
      title: "详情",
      chunks: ['chunk-vendors', 'chunk-common', 'stock']
    },
    result: {
      entry: "./src/pages/result/result.js",
      template: "./src/pages/result/result.html",
      filename: "result.html",
      title: "结果页",
      chunks: ['chunk-vendors', 'chunk-common', 'result']
    },
    jianli: {
      entry: "./src/pages/jianli/jianli.js",
      template: "./src/pages/jianli/jianli.html",
      filename: "jianli.html",
      title: "简历",
      chunks: ['chunk-vendors', 'chunk-common', 'jianli']
    }
  },
  outputDir: 'dist',
  assetsDir: 'common',
  productionSourceMap: false, // 打包不生成map
  chainWebpack: config => {
    config.output
      .filename(() => {
        return '[name]/js/[name].js'
      })
 
    config.plugin('extract-css').tap(options => {
      options[0].filename = '[name]/css/[name].[hash:8].css'
      return options
    })

 
    // 使用splitChunks抽离公用代码
    config.optimization.splitChunks({
      cacheGroups: {
        common: {
          name: 'chunk-common', // 打包后的文件名
          chunks: 'initial', //
          minChunks: 2,
          maxInitialRequests: 5,
          minSize: 0,
          priority: 1,
          reuseExistingChunk: true
        },
        vendors: {
          name: 'chunk-vendors',
          test: /[\\/]node_modules[\\/]/,
          chunks: 'initial',
          priority: 2,
          reuseExistingChunk: true,
          enforce: true
        }
      }
    })
  },
  configureWebpack: {
 
  },
  css: {
    extract: true,
    requireModuleExtension: true,
    sourceMap: false
  }
}